
# 两两交换链表中的节点



class ListNode:
    
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next


class Solution:

    def swapPairs(self, head):
        if not (head and head.next):
            return head
        p = ListNode(-1)

        cur, head, stack = head, p, []

        while cur and cur.next:
            stack.append(cur),stack.append(cur.next)
            cur = cur.next.next

            p.next = stack.pop()
            p.next.next = stack.pop()

            p = p.next.next

        if cur:
            p.next = cur

        else:
            p.next = None
        
        return head.next



